<template>
  <div class="promotion-container">
    <h1>非遗宣传</h1>
    
    <!-- 搜索区域 -->
    <div class="search-section">
      <el-input
        v-model="searchParams.title"
        placeholder="请输入视频标题搜索"
        class="search-input"
        @keyup.enter="fetchVideoList"
      />
            <el-button type="primary" @click="fetchVideoList"  style="margin-left: 10px;">搜索</el-button>
            <el-button @click="resetSearch"  style="margin-left: 10px;">重置</el-button>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 视频列表区域 -->
      <div class="video-list" v-loading="loading">
        <div class="video-card" v-for="video in videoList" :key="video.id">
          <div class="video-cover-container" @click="playVideo(video)">
            <el-image
              :src="video.coverImage || 'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg'"
              class="video-cover"
              fallback-src="https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg"
            />
            <div class="video-play-icon">
              <i class="el-icon-video-play"></i>
            </div>
          </div>
          <div class="video-info">
            <h3 class="video-title">{{ video.title }}</h3>
            <div class="video-meta">
              <span>浏览量: {{ video.viewCount || 0 }}</span>
              <span>{{ formatDate(video.createTime) }}</span>
            </div>
          </div>
        </div>
        <div class="empty-state" v-if="videoList.length === 0 && !loading">
          暂无视频数据
        </div>
      </div>

      <!-- 视频热度排行榜 -->
      <div class="hot-ranking">
        <div class="ranking-title">
          <div class="title-icon">
            <i class="el-icon-s-data"></i>
          </div>
          <h3>热门视频排行榜</h3>
        </div>
        <div class="ranking-list">
          <div 
            v-for="(video, index) in topVideos" 
            :key="video.id"
            class="ranking-item"
            @click="playVideo(video)"
          >
            <!-- 前三名使用特殊样式 -->
            <div v-if="index < 3" class="rank-number special-rank" :class="getRankClass(index)">
              <i v-if="index === 0" class="el-icon-crown"></i>
              <i v-else-if="index === 1" class="el-icon-medal-1"></i>
              <i v-else-if="index === 2" class="el-icon-medal-2"></i>
            </div>
            <!-- 其他排名使用数字 -->
            <div v-else class="rank-number normal-rank">
              {{ index + 1 }}
            </div>
            <div class="rank-info">
              <div class="rank-title">{{ video.title }}</div>
              <div class="rank-meta">
                <i class="el-icon-view"></i>
                <span>{{ video.viewCount || 0 }}</span>
                <span class="view-label">次观看</span>
              </div>
            </div>
            <div class="rank-tag" :class="`tag-top${index + 1}`" v-if="index < 3">
              Top{{ index + 1 }}
            </div>
          </div>
        </div>
        <!-- 排行榜底部装饰 -->
        <div class="ranking-footer">
          <div class="footer-decoration"></div>
        </div>
      </div>
    </div>

    <!-- 分页组件 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="searchParams.current"
        v-model:page-size="searchParams.size"
        :page-sizes="[10, 20, 50]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="fetchVideoList"
        @current-change="fetchVideoList"
      />
    </div>
  </div>


</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { getVideoList, getTopVideos } from '@/api/video';
import { ElMessage } from 'element-plus';

const router = useRouter();

// 状态管理
const videoList = ref([]);
const total = ref(0);
const loading = ref(false);
const topVideos = ref([]);
const topVideosLoading = ref(false);

// 搜索参数
const searchParams = ref({
  current: 1,
  size: 10,
  title: ''
});



// 格式化日期
const formatDate = (timestamp) => {
  if (!timestamp) return '';
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
};

// 获取视频列表
const fetchVideoList = async () => {
  loading.value = true;
  try {
    const response = await getVideoList(searchParams.value);
    
    if (response.success && Array.isArray(response.data)) {
      videoList.value = response.data
        .filter(video => video.status === 1) // 只显示状态为1的视频
        .map(video => ({
          id: video.id,
          title: video.title || '',
          videoUrl: video.videoUrl || '',
          coverImage: video.coverImage,
          description: video.description || '',
          status: video.status,
          viewCount: video.viewCount || 0,
          createTime: video.createTime
        }));
      total.value = response.total || 0;
    } else {
      ElMessage.error('数据格式错误');
      videoList.value = [];
      total.value = 0;
    }
  } catch (error) {
    console.error('视频列表请求异常:', error);
    ElMessage.error('获取视频列表异常，请重试');
  } finally {
    loading.value = false;
  }
};

// 播放视频 - 跳转到视频详情页面
const playVideo = (video) => {
  router.push({ name: 'VideoDetail', params: { id: video.id } });
};

// 重置搜索
const resetSearch = () => {
  searchParams.value = {
    current: 1,
    size: 10,
    title: ''
  };
  fetchVideoList();
};

// 获取热门视频列表
  const fetchTopVideos = async () => {
    topVideosLoading.value = true;
    try {
      const response = await getTopVideos();
      if (response.success && Array.isArray(response.data)) {
        topVideos.value = response.data.filter(video => video.status === 1); // 只显示状态为1的视频
      } else {
        ElMessage.error('获取热门视频失败');
        topVideos.value = [];
      }
    } catch (error) {
      console.error('热门视频请求异常:', error);
      ElMessage.error('获取热门视频异常，请重试');
    } finally {
      topVideosLoading.value = false;
    }
  };

// 获取排名样式类
const getRankClass = (index) => {
  if (index === 0) return 'rank-gold';
  if (index === 1) return 'rank-silver';
  if (index === 2) return 'rank-bronze';
  return '';
};

// 页面加载时获取数据
onMounted(() => {
  fetchVideoList();
  fetchTopVideos();
});
</script>

<style scoped>
.promotion-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.search-section {
  margin-bottom: 20px;
}

.search-input {
  width: 600px;
}

/* 主要内容区域布局 */
.main-content {
  display: flex;
  gap: 20px;
  margin-top: 20px;
}

/* 视频列表区域 */
.video-list {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

/* 视频热度排行榜 */
.hot-ranking {
  width: 300px;
  background: linear-gradient(135deg, #f8f9ff 0%, #e6f0ff 100%);
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 4px 20px rgba(64, 158, 255, 0.15);
  border: 1px solid #e3f2fd;
  height: fit-content;
  position: relative;
  overflow: hidden;
}

/* 排行榜背景装饰 */
.hot-ranking::before {
  content: '';
  position: absolute;
  top: -50px;
  right: -50px;
  width: 150px;
  height: 150px;
  background: radial-gradient(circle, rgba(64, 158, 255, 0.1) 0%, rgba(64, 158, 255, 0) 70%);
  border-radius: 50%;
  z-index: 0;
}

.ranking-title {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  position: relative;
  z-index: 1;
  font-size: 0;
  font-weight: normal;
}

.title-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #409eff, #67c23a);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.title-icon i {
  font-size: 20px;
  color: white;
}

.ranking-title h3 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  color: #1a5276;
  background: linear-gradient(90deg, #1a5276, #3498db);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.ranking-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  position: relative;
  z-index: 1;
}

.ranking-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  background-color: white;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  position: relative;
  overflow: hidden;
}

.ranking-item::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
  background: linear-gradient(to bottom, #409eff, #67c23a);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.ranking-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 6px 20px rgba(64, 158, 255, 0.25);
  background-color: #ffffff;
}

.ranking-item:hover::after {
  opacity: 1;
}

/* 排名样式 */
.rank-number {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  margin-right: 16px;
  flex-shrink: 0;
}

/* 特殊排名样式 */
.special-rank {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 20px;
  color: white;
}

.special-rank i {
  font-size: 24px;
}

.rank-gold {
  background: linear-gradient(135deg, #FF4500, #FF6347);
  box-shadow: 0 3px 10px rgba(255, 69, 0, 0.4);
}

.rank-silver {
  background: linear-gradient(135deg, #4169E1, #0000FF);
  box-shadow: 0 3px 10px rgba(65, 105, 225, 0.4);
}

.rank-bronze {
  background: linear-gradient(135deg, #800080, #9400D3);
  box-shadow: 0 3px 10px rgba(128, 0, 128, 0.4);
}

/* 普通排名样式 */
.normal-rank {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  background-color: #f0f2f5;
  color: #606266;
  font-size: 16px;
}

.rank-info {
  flex: 1;
  min-width: 0;
}

.rank-title {
  font-size: 15px;
  font-weight: 500;
  color: #303133;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 8px;
  line-height: 1.4;
}

.ranking-item:hover .rank-title {
  color: #409eff;
}

.rank-meta {
  display: flex;
  align-items: center;
  font-size: 13px;
  color: #909399;
}

.rank-meta i {
  margin-right: 5px;
  color: #409eff;
}

.view-label {
  margin-left: 5px;
  color: #c0c4cc;
}

/* 排名标签 */
.rank-tag {
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 15px;
  font-weight: 600;
  flex-shrink: 0;
  transition: all 0.3s ease;
}

.tag-top1 {
  background: linear-gradient(135deg, #fff3cd, #ffeaa7);
  color: #856404;
  border: 1px solid #ffeaa7;
}

.tag-top2 {
  background: linear-gradient(135deg, #d6d8db, #e9ecef);
  color: #495057;
  border: 1px solid #e9ecef;
}

.tag-top3 {
  background: linear-gradient(135deg, #d1ccc0, #a09a88);
  color: #5d4037;
  border: 1px solid #d1ccc0;
}

/* 排行榜底部装饰 */
.ranking-footer {
  margin-top: 20px;
  position: relative;
  z-index: 1;
}

.footer-decoration {
  height: 3px;
  background: linear-gradient(90deg, #409eff, #67c23a, #e6a23c);
  border-radius: 3px;
}

/* 移动端响应式 */
@media (max-width: 1024px) {
  .main-content {
    flex-direction: column;
  }
  
  .hot-ranking {
    width: 100%;
    order: -1;
  }
}

.video-card {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.2s;
}

.video-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.video-cover-container {
  position: relative;
  cursor: pointer;
}

.video-cover {
  width: 100%;
  height: 160px;
  object-fit: cover;
}

.video-play-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 40px;
  height: 40px;
  background-color: rgba(0, 0, 0, 0.5);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  opacity: 0;
  transition: opacity 0.2s;
}

.video-card:hover .video-play-icon {
  opacity: 1;
}

.video-info {
  padding: 12px;
}

.video-title {
  font-size: 16px;
  margin-bottom: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.video-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #666;
}

.empty-state {
  text-align: center;
  padding: 40px;
  color: #999;
}

.pagination {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}

@media (max-width: 768px) {
  .search-input {
    width: 100%;
  }
  
  .video-list {
    grid-template-columns: 1fr;
  }
  
  .hot-ranking {
    padding: 12px;
  }
  
  .ranking-title {
    font-size: 16px;
  }
}

h1 {
  color: #1a5276;
  margin-bottom: 20px;
  text-align: center;
}
</style>